/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.nnaplanner.DAL;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/**
 *
 * @author david
 */
public final class DAL {
    private static final PersistenceManagerFactory pmfInstance = 
            JDOHelper.getPersistenceManagerFactory("transactions-optional");

    private DAL() {
    }
    
    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }


    public static Query newQuery(){
        PersistenceManager pm = get().getPersistenceManager();
        return pm.newQuery();
    }

    public static <T> void save(T t) {
        PersistenceManager pm = get().getPersistenceManager();
        pm.makePersistent(t);
        pm.close();
    }

    public static <T> T getByID(T t, String id){
        PersistenceManager pm = get().getPersistenceManager();
        T u = (T) pm.getObjectById(t.getClass(), id);
        pm.close();
        return u;
    }

     public static <T> void delete(T t, String id) {
        PersistenceManager pm = get().getPersistenceManager();
        pm.deletePersistent(pm.getObjectById(t.getClass(), id));
        pm.close();
    }

}
